/**
 * Created by shen'jia.
 */
;(function() {
  'use strict';
  angular.module('controllers.oldOMDisplay.oldHealthy')
    .controller('oldHealthyCurrentController', current)
    .controller('oldHealthyhistoryController', history);

  function current($scope, oldHealthyData, oldHealthyServer, chartOption, COREDATA, $timeout, CoreLoading, CoreService,PublicFn) {
    $scope.businessLine = COREDATA.BUSINESSLINE;
    $scope.provinces = COREDATA.PROVINCES;
    $scope.timeTypes = [{
      timeTypeName: '小时',
      timeTypeCode: '0'
    }, {
      timeTypeName: '天',
      timeTypeCode: '1'
    }];
    $scope.sendData = oldHealthyData.currentSendData;
    $scope.getBL = function() {
      $timeout(function() {
        var bl = {
          businessLine: []
        };
        if ($scope.sendData.businessLine !== undefined) {
          bl.businessLine.push($scope.sendData.businessLine.businessLineCode);
        }
        if (bl.businessLine.length !== 0) {
          oldHealthyServer.queryTradeType(bl, function(res) {
            if (res.data.length !== 0) {
              $scope.tradeTypes = res.data;
              $scope.close.tradeType = true;
            }
          });
        } else {
          $scope.close.tradeType = false;
        }
      });
      $scope.sendData.tradeTypeCode = [];
    };

    $scope.shoumun = {
      flag: 1
    };

    $scope.isBU = false;
    var currentEcharts = echarts.init(document.getElementById('current'));
    $scope.searchST = function() {
      if ($scope.shoumun.flag === 1) {
        return;
      }
      var params = formatterSendData($scope.sendData);
      var timeTitle = PublicFn.fmtDateTitle(params.code,'timeTypeCode');
      var title = timeTitle +
        params.name.businessName.toString() +
        params.name.provinceName.toString() +
        '健康度';
      CoreLoading.setState(true);
      oldHealthyServer.current(params.code, function(res) {
        if (res.state === '10000' && res.data.length >= 1) {
          currentEcharts.clear();
          var options = getEchartsData(res.data,title);
          currentEcharts.setOption(options);
          $scope.isBU = true;
        } else {
          $scope.shoumun.flag = 1;
          $scope.isBU = false;
          CoreService.dialog(res.message);
        }
      });
      CoreLoading.setState(false);
    };

    function getEchartsData(data,title) {
      var temp = {
        title: title,
        legendName: [],
        xAxisData: [],
        seriesData: []
      };
      var obj = {};
      angular.forEach(data, function(d) {
        if (temp.xAxisData.indexOf(d.provinceName) === -1) {
          temp.xAxisData.push(d.provinceName);
        }
        angular.forEach(d.data, function(dd) {
          if (temp.legendName.indexOf(dd.channelName) === -1) {
            temp.legendName.push(dd.channelName);
          }
          if (!obj[dd.channelName]) {
            obj[dd.channelName] = [dd.channelHealth];
          } else {
            obj[dd.channelName].push(dd.channelHealth);
          }
        });
      });

      angular.forEach(temp.legendName, function(lgn, index) {
        temp.seriesData[index] = {
          name: lgn,
          type: 'bar',
          data: obj[lgn]
        };
      });
      return {
        title: {
          text: temp.title,
          x: 'center'
        },
        animation: false,
        dataZoom:{
          show:true
        },
        tooltip: {
          trigger: 'item',
          formatter: function(params) {
            var name = params.name;
            var seriesName = params.seriesName;
            var value = params.value;
            return seriesName + ' </br>' + name + ':' + value + '%';
          }
        },
        grid: {
          x2: 100
        },
        legend: {
          data: temp.legendName,
          orient: 'vertical',
          x: 'right',
          y: 'center'
        },
        xAxis: [{
          type: 'category',
          data: temp.xAxisData,

        }],
        yAxis: [{
          type: 'value',
          min:90,
          max:100,
          axisLabel: {
            formatter: '{value}%'
          }
        }],
        series: temp.seriesData
      };
    }

    function formatterSendData(sendData) {
      var send = {
        code: {
          businessLine: [],
          provinceCode: [],
          tradeTypeCode: [],
          systemCode: '1',
          timeTypeCode: '',
          startTime: '',
          endTime: ''
        },
        name: {
          businessName: [],
          provinceName: [],
        }
      };
      if (!sendData.businessLine) {
        send.code.businessLine = $scope.businessLine.map(function(bl) {
          return bl.businessLineCode;
        });
        send.name.businessName = $scope.businessLine.map(function(bl) {
          return bl.businessLineName;
        });
      } else {
        send.code.businessLine.push(sendData.businessLine.businessLineCode);
        send.name.businessName.push(sendData.businessLine.businessLineName);
      }

      if (sendData.provinceCode.length === 0) {
        send.code.provinceCode = $scope.provinces.map(function(pv) {
          return pv.provinceCode;
        });
        send.name.provinceName = ['不同省份'];
      } else {
        send.code.provinceCode = sendData.provinceCode.map(function(pv) {
          return pv.provinceCode;
        });
        if (sendData.provinceCode.length <= 5) {
          send.name.provinceName = sendData.provinceCode.map(function(pv) {
            return pv.provinceName;
          });
        }
      }

      if (sendData.tradeTypeCode.length !== 0) {
        send.code.tradeTypeCode = sendData.tradeTypeCode.map(function(ttc) {
          return ttc.tradeTypeCode;
        });
      }
      if (sendData.timeTypeCode === '1') {
        send.code.startTime = CoreService.dateFormat(sendData.startTime, 'yyyy-MM-dd');
        send.code.endTime = CoreService.dateFormat(sendData.endTime, 'yyyy-MM-dd');
      } else {
        send.code.startTime = CoreService.dateFormat(sendData.startTime, 'yyyy-MM-dd hh');
        send.code.endTime = CoreService.dateFormat(sendData.endTime, 'yyyy-MM-dd hh');
      }
      send.code.timeTypeCode = sendData.timeTypeCode;
      return send;
    }
  }

  function history($scope, oldHealthyData, oldHealthyServer, chartOption) {
    $scope.businessCha = function() {
      if ($scope.sendData.channelName === '银行总对总') {
        $scope.selector.business = [{
          businessCode: '',
          businessName: '全部'
        }, {
          businessCode: '02',
          businessName: '缴费'
        }];
      } else if ($scope.sendData.channelName === '全部') {
        $scope.selector.business = [{
          businessCode: '',
          businessName: '全部'
        }];
      } else {
        $scope.selector.business = [{
          businessCode: '',
          businessName: '全部'
        }, {
          businessCode: '01',
          businessName: '充值'
        }];
      }

    };
    $scope.shoumun = {
      flag: 1
    };
    $scope.selector = oldHealthyData.selectData; //下拉列表的数据
    $scope.sendData = oldHealthyData.historySendData;
    $scope.isBU = false;

    var BUEcharts = echarts.init(document.getElementById('history_xiazuan'));
    var BUoption = chartOption.historyBusinessOp;
    var BUtitles = '';

    var historyEcharts = echarts.init(document.getElementById('history'));
    var option = chartOption.historyOp;
    var titles = '';
    historyEcharts.on(echarts.config.EVENT.CLICK, function() {
      oldHealthyServer.historyBusiness(BUEcharts, BUoption, BUtitles);
      $scope.isBU = true;
    });

    $scope.searchST = function() {
      if ($scope.shoumun.flag === 1) {
        return;
      }
      $scope.isBU = false;
      oldHealthyServer.history(historyEcharts, option, titles);
    };

  }

})();